import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class StackedLineModel {
  final Color color;
  final double width;
  final Text text;
  final Image? indicatorImg;

  StackedLineModel({
    this.color = Colors.cyan,
    this.width = 1,
    this.text = const Text(''),
    this.indicatorImg,
  });
}

class StackedLineIndicator extends StatelessWidget {
  StackedLineIndicator({
    super.key,
    this.minHeight = 8,
    this.space = 0,
    required this.stackedItems,
    this.borderRadiusApplyToAll = false,
    this.borderRadius = BorderRadius.zero,
  });

  final double minHeight;
  final double space;
  final bool borderRadiusApplyToAll;
  late BorderRadius? borderRadius;
  final List<StackedLineModel> stackedItems;

  @override
  Widget build(BuildContext context) {
    List<SizedBox> boxList = [];

    for (int i = 0; i < stackedItems.length; i++) {
      StackedLineModel model = stackedItems[i];

      BorderRadius temp = borderRadius!;
      if (!borderRadiusApplyToAll) {
        if (i == 0) {
          temp = BorderRadius.only(topLeft: borderRadius!.topLeft, bottomLeft: borderRadius!.bottomLeft);
        } else if (i == stackedItems.length - 1) {
          temp = BorderRadius.only(topRight: borderRadius!.topRight, bottomRight: borderRadius!.bottomRight);
        } else {
          temp = BorderRadius.zero;
        }
      }

      boxList.addAll([
        SizedBox(
          width: model.width,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              if (model.text.data!.isNotEmpty) model.text,
              // 撑空间
              SizedBox(height: 10.r),
              LinearProgressIndicator(
                value: 1,
                borderRadius: temp,
                minHeight: minHeight,
                backgroundColor: model.color,
                valueColor: AlwaysStoppedAnimation(model.color),
              ),
              if (model.indicatorImg != null) model.indicatorImg!,
            ],
          ),
        ),
        if (space > 0) SizedBox(width: space),
      ]);
    }
    return Row(mainAxisAlignment: MainAxisAlignment.center, children: boxList);
  }
}
